package com.zynn.common.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author 刘天元
 * @description 类型转换工具
 * @create 2018-08-01 14:00
 **/
public class TypeConvertUtil {
    /**
     * object转Long
     * @param object
     * @return
     */
    public static Long objectToLong(Object object) {
        return Long.parseLong(Objects.toString(object, "0"));
    }

    /**
     * object转Long
     * @param object
     * @return
     */
    public static Date objectToDate(Object object) {
        // 设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(Objects.toString(object, "0"));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * object转String
     * @param object
     * @return
     */
    public static Double objectToDouble(Object object) {
        return Double.parseDouble(Objects.toString(object, "0"));
    }

    /**
     * object转Integer
     * @param object
     * @return
     */
    public static Integer objectToInteger(Object object) {
        return Integer.parseInt(Objects.toString(object, "0"));
    }

    /**
     * object转String
     * @param object
     * @return
     */
    public static String objectToString(Object object) {
        String str = Objects.toString(object, "");
        String nullStr = "null";
        if(nullStr.equals(str)) {
            str = "";
        }
        return str;
    }
}
